package tt.dz.egobus.servicemanager.entities;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import tt.dz.egobus.base.entity.BaseEntity;
import tt.dz.egobus.bean.CodeStatus;

import javax.persistence.*;

/**
 * @author Anson
 * @date 17-3-30
 * @name 业务异常上报
 * @since 1.0.3
 */
@Entity
@Table(name = "business_exception_alert", schema = "servicemanager")
public class BusinessExceptionAlert extends BaseEntity {

    /**
     * 车辆存在未处理的异常情况
     */
    public static final String IS_EXIST_VALIDATE_BUS_UNDEAL = " select count(1)   from BusinessExceptionAlert t  where 1=1 " +
            "   and   busNo=:busNo  " +
            "   and  t.status in (  " +
            "        " + ExceptionAlertStatus.ALERT.getCode() + " , " + ExceptionAlertStatus.ACCEPT.getCode() + " , " +
            "       " + ExceptionAlertStatus.DEAL.getCode() + "  ) ";


    public static enum ExceptionAlertStatus implements CodeStatus {

        ALERT(0, "上报"), ACCEPT(10, "接受"), DEAL(20, "正处理"), FINISH(30, "处理完毕"), CANCEL(40, "取消"),;
        /**
         * 状态 , 0:待处理(上报);10:接受;20:正处理;30:处理完毕;40:取消
         * 页面呈现： 0：待接受 10 待处理 20 处理中 30 已处理 40 驳回
         */
        private final int code;
        private final String msg;

        ExceptionAlertStatus(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        @Override
        public int getCode() {
            return code;
        }

        /**
         * @param code 状态码
         * @author Anson
         * @date 17-3-27
         * @name 根据状态码或去枚举
         * @since 1.0.2
         */
        public static final ExceptionAlertStatus getStatus(int code) {
            for (ExceptionAlertStatus status : ExceptionAlertStatus.values()) {
                if (status.getCode() == code) {
                    return status;
                }
            }
            return null;
        }

        public static String getStatusMsg(int code) {
            ExceptionAlertStatus status = getStatus(code);
            return status == null ? "" : status.getMsg();
        }

    }


    /**
     * @author Anson
     * @date 17-3-30
     * @name 系统上报状态类型
     * @since 1.0.3
     */
    public static enum ExceptionAlertType implements CodeStatus {
        //异常类型 1:系统 2:APP 3:车辆 4:其他
        SYSTEM(1, "系统"), APP(2, "APP"), BUS(3, "车辆"), OTHER(4, "其他"),;


        private final int code;
        private final String msg;

        ExceptionAlertType(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        @Override
        public int getCode() {
            return code;
        }

        /**
         * @param code 状态码
         * @author Anson
         * @date 17-3-27
         * @name 根据状态码或去枚举
         * @since 1.0.2
         */
        public static final ExceptionAlertType getStatus(int code) {
            for (ExceptionAlertType status : ExceptionAlertType.values()) {
                if (status.getCode() == code) {
                    return status;
                }
            }
            return null;
        }

        public static String getStatusMsg(int code) {
            ExceptionAlertType status = getStatus(code);
            return status == null ? "" : status.getMsg();
        }
    }

    public boolean assertAlert() {
        return this.status == ExceptionAlertStatus.ALERT.getCode();
    }

    public boolean assertAccept() {
        return this.status == ExceptionAlertStatus.ACCEPT.getCode();
    }

    public boolean assertDeal() {
        return this.status == ExceptionAlertStatus.DEAL.getCode();
    }

    public boolean assertFinish() {
        return this.status == ExceptionAlertStatus.FINISH.getCode();
    }

    public boolean assertCancel() {
        return this.status == ExceptionAlertStatus.CANCEL.getCode();
    }


    public boolean assertSystem() {
        return this.exceptionType == ExceptionAlertType.SYSTEM.getCode();
    }

    public boolean assertApp() {
        return this.exceptionType == ExceptionAlertType.APP.getCode();
    }

    public boolean assertBus() {
        return this.exceptionType == ExceptionAlertType.BUS.getCode();
    }

    public boolean assertOther() {
        return this.exceptionType == ExceptionAlertType.OTHER.getCode();
    }


    /**
     * 设置系统异常类别
     */
    public void setAbleSystem() {
        this.exceptionType = ExceptionAlertType.SYSTEM.getCode();
    }

    /**
     * 设置app异常类别
     */
    public void setAbleApp() {
        this.exceptionType = ExceptionAlertType.APP.getCode();
    }

    /**
     * 设置车辆异常类别
     */
    public void setAbleBus() {
        this.exceptionType = ExceptionAlertType.BUS.getCode();
    }

    /**
     * 设置其他异常类别
     */
    public void setAbleOther() {
        this.exceptionType = ExceptionAlertType.OTHER.getCode();
    }


    /**
     * 司机名称
     */
    @Column(name = "driver_name")
    @NotBlank(message = "司机名称不能为空")
    @Length(max = 50, message = "车牌号长度不能超过50")
    private String driverName;


    /**
     * 手机号码
     */
    @Column(name = "driver_mobile")
    @NotBlank(message = "司机名称不能为空")
    @Length(max = 15, message = "车牌号长度不能超过15")
    private String driverMobile;

    /**
     * 车牌号
     */
    @Column(name = "bus_no")
    @Length(max = 255, message = "车牌号长度不能超过255")
    private String busNo;

    /**
     * 上报地点经度坐标
     */
    @Column(name = "x")
    private double x;

    /**
     * 上报地点纬度坐标
     */
    @Column(name = "y")
    private double y;

    /**
     * 上报的地理位置名称
     */
    @Column(name = "address")
    @Length(max = 255, message = "上报的地理位置名称长度不能超过255")
    private String address;

    /**
     * 异常信息
     */
    @Column(name = "exception")
    @NotBlank(message = "异常信息不能为空")
    @Length(max = 255, message = "异常信息长度不能超过255")
    private String exception;

    /**
     * 异常类型 1:系统 2:APP 3:车辆 4:其他
     */
    @Column(name = "exception_type")
    private int exceptionType = ExceptionAlertType.BUS.getCode();

    /**
     * 状态 , 0:上报;10:接受;20:正处理;30:处理完毕;40:取消
     */
    @Column(length = 10)
    private int status = 0;

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getDriverMobile() {
        return driverMobile;
    }

    public void setDriverMobile(String driverMobile) {
        this.driverMobile = driverMobile;
    }

    public String getBusNo() {
        return busNo;
    }

    public void setBusNo(String busNo) {
        this.busNo = busNo;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public int getExceptionType() {
        return exceptionType;
    }

    public void setExceptionType(int exceptionType) {
        this.exceptionType = exceptionType;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BusinessExceptionAlert)) return false;

        BusinessExceptionAlert alert = (BusinessExceptionAlert) o;

        if (Double.compare(alert.getX(), getX()) != 0) return false;
        if (Double.compare(alert.getY(), getY()) != 0) return false;
        if (getExceptionType() != alert.getExceptionType()) return false;
        if (getStatus() != alert.getStatus()) return false;
        if (getDriverName() != null ? !getDriverName().equals(alert.getDriverName()) : alert.getDriverName() != null)
            return false;
        if (getDriverMobile() != null ? !getDriverMobile().equals(alert.getDriverMobile()) : alert.getDriverMobile() != null)
            return false;
        if (getBusNo() != null ? !getBusNo().equals(alert.getBusNo()) : alert.getBusNo() != null) return false;
        if (getAddress() != null ? !getAddress().equals(alert.getAddress()) : alert.getAddress() != null) return false;
        return getException() != null ? getException().equals(alert.getException()) : alert.getException() == null;

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = getDriverName() != null ? getDriverName().hashCode() : 0;
        result = 31 * result + (getDriverMobile() != null ? getDriverMobile().hashCode() : 0);
        result = 31 * result + (getBusNo() != null ? getBusNo().hashCode() : 0);
        temp = Double.doubleToLongBits(getX());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(getY());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (getAddress() != null ? getAddress().hashCode() : 0);
        result = 31 * result + (getException() != null ? getException().hashCode() : 0);
        result = 31 * result + getExceptionType();
        result = 31 * result + getStatus();
        return result;
    }

    @Override
    public String toString() {
        return "BusinessExceptionAlert{" +
                "driverName='" + driverName + '\'' +
                ", driverMobile='" + driverMobile + '\'' +
                ", busNo='" + busNo + '\'' +
                ", x=" + x +
                ", y=" + y +
                ", address='" + address + '\'' +
                ", exception='" + exception + '\'' +
                ", exceptionType=" + exceptionType +
                ", status=" + status +
                '}';
    }
}
